package com.gougou.front.web.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.gougou.common.util.CookieUtils;
import com.gougou.front.pojo.*;
import com.gougou.front.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2018-09-07
 */
@RestController
@RequestMapping("/tbOrder")
public class TbOrderController {
    @Autowired
    private MPTbOrderItemService orderItemService;

    @Autowired
    private MPTbItemService itemService;
    @Autowired
    private MPTbUserService userService;
    @Autowired
    private MPTbOrderService orderService;
    @Autowired
    private MPTbOrderShippingService orderShippingService;

    @Autowired
    private MPTbCartService cartService;

    private static final String COOKIE_NAME = "TT_TOKEN";
    //跳转到购物车
    @RequestMapping(value = "/goCarts")
    public ModelAndView toCart(){
        ModelAndView mv = new ModelAndView("views/cart");
        EntityWrapper<TbCart> wr = new EntityWrapper<>();
        wr.where("user_id={0}",8);
        List<TbCart> tbCarts = cartService.selectList(wr);
        mv.addObject("cartList",tbCarts);
        return  mv;
    }
    //提交订单
    @RequestMapping(value = "/submit")
    public Map<String,Object> submitOrder(TbOrder order, HttpServletRequest request){
        Map<String,Object> result = new HashMap<String, Object>();


//        TbUser user = userService.selectById(token);
        //拿到时间信息与用户id创建订单编号
        Date date = new Date();
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyyMMddHHmmss");
        String orderId =simpleDateFormat.format(date)+8;
        String cookie =  CookieUtils.getCookieValue(request,COOKIE_NAME);
        //将用户登录信息填入订单信息
        order.setUserId(Long.valueOf(8));
        order.setOrderId(orderId);
        order.setBuyerNick("zhagnsan");
        order.setCreateTime(date);
        order.setUpdateTime(date);
        order.setStatus(1);
        //将订单ID放入orderItem
        List<TbOrderItem> list = order.getOrderItems();
        for (TbOrderItem tbOrderItem : list){
            tbOrderItem.setOrderId(orderId);
        }
        TbOrderShipping shipping = order.getOrderShipping();
        //将订单ID放入shipping
        shipping.setOrderId(orderId);
        TbOrder tbOrder = this.TakeOutInfo(order);
        try {
            //将订单信息加入数据库
            if (orderService.insert(tbOrder)==true){
                //将订单商品信息加入数据库
                orderItemService.insertBatch(list);
                //将配送信息加入数据库
                orderShippingService.insert(shipping);
                result.put("status",200);
                result.put("data",orderId);

            }
            return result;

        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            return  result;

        }
    }
    //将订单表的数据取出来
    public TbOrder TakeOutInfo(TbOrder tbOrder){
        TbOrder order = new TbOrder();
        order.setOrderId(tbOrder.getOrderId());
        order.setCreateTime(tbOrder.getCreateTime());
        order.setUpdateTime(tbOrder.getUpdateTime());
        order.setBuyerNick(tbOrder.getBuyerNick());
        order.setUserId(tbOrder.getUserId());
        order.setBuyerRate(tbOrder.getBuyerRate());
        order.setBuyerMessage(tbOrder.getBuyerMessage());
        order.setCloseTime(tbOrder.getCloseTime());
        order.setConsignTime(tbOrder.getConsignTime());
        order.setEndTime(tbOrder.getEndTime());
        order.setPayment(tbOrder.getPayment());
        order.setPaymentTime(tbOrder.getPaymentTime());
        order.setPaymentType(tbOrder.getPaymentType());
        order.setPostFee(tbOrder.getPostFee());
        order.setShippingCode(tbOrder.getShippingCode());
        order.setShippingName(tbOrder.getShippingName());
        order.setStatus(tbOrder.getStatus());
        return order;
    }
    @RequestMapping("/goSuccessView")
    public ModelAndView goSuccessView(Long id){
        ModelAndView mv = new ModelAndView("views/success");
//        EntityWrapper<TbOrder> wrapper = new EntityWrapper<TbOrder>();
//        wrapper.where("order_id={0}",id);
//        TbOrder order = orderService.selectOne(wrapper);
        mv.addObject("order","201809252031287");
        mv.addObject("date","两天后");
        return  mv;

    }

}

